package com.hskn.hss.utils;


import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataPager {

    public static List dataPager(JSONObject params, List list,Map pageMap) {
        Integer page = StringUtils.isEmpty(params.getInteger("page")) ? 1 : params.getInteger("page");
        Integer size = StringUtils.isEmpty(params.getInteger("size")) ? 10 : params.getInteger("size");
        int total = list.size();
        int pageCount = total / size;
        int fromIndex = size * (page - 1);
        int toIndex = fromIndex + size;
        if (toIndex >= total) {
            toIndex = total;
        }
        if (page > pageCount + 1) {
            fromIndex = 0;
            toIndex = 0;
        }

        pageMap.put("total", total);
        pageMap.put("size", size);
        pageMap.put("pages", page);

        return list.subList(fromIndex, toIndex);
    }
}
